<?php

class DragGraphicVerificationCode{

    public $jsonSrc;
    public $imgRoute = 'http://enjoy-web.oss-cn-shenzhen.aliyuncs.com/static/code';

    public function BgImage($param = ['key'=>'','overtime'=>5]){
        // 源文件
        $filename = $this->imgRoute.'/code('.rand(1, 30).').png';
        // 设置最大宽高
        $width = 260*3;
        $height = 160*3;
        header('Content-Type: image/jpeg');
        // 重新取样
        $img = imagecreatetruecolor($width, $height);
        $imagefile = imagecreatefrompng($filename);
        imagecopyresampled($img, $imagefile, 0, 0, 0, 0, $width, $height, $width, $height);

        $y =  130;
        $x = rand(100, $width-210);

        $txtData = '[]';

        try {
            if(file_exists($this->jsonSrc.'DragGraphicVerificationCodeCheck.json')){
                $txtData = file_get_contents($this->jsonSrc.'DragGraphicVerificationCodeCheck.json');
            }
        } catch (Exception $exception) { }

        //文件加锁解决并发的问题
        $txtJson = json_decode($txtData,true);
        $txtJson[$param['key'].$_SERVER['REMOTE_ADDR']] = ['src'=>$filename,'code'=>$x,'time'=>time()+60*$param['overtime']];
        $fileName = $this->jsonSrc.'DragGraphicVerificationCodeCheck.json';
        $dataToSave = json_encode($txtJson);
        if($fp=fopen($fileName,'w')){
            $startTime=microtime();
            do{
                $canWrite=flock($fp,LOCK_EX);
                if(!$canWrite){
                    usleep(round(rand(0,100)*1000));
                }
            }while((!$canWrite)&&((microtime()-$startTime)<1000));
            if($canWrite){
                fwrite($fp,$dataToSave);
            }
            fclose($fp);
        }

        imagecopyresampled($img, imagecreatefrompng('http://enjoy-web.oss-cn-shenzhen.aliyuncs.com/static/VerificationCode2.png'), $x, $y, 0, 0,210,210,210,210);

        // 输出
        imagejpeg($img, null, 100);
    }

    public function BlockImage($keyName){

        // 设置最大宽高
        $width = 210;
        $height = 210;

        // Content type
        header('Content-Type: image/jpeg');

        $txtData = '["'.$keyName.$_SERVER['REMOTE_ADDR'].'"=>["code"=>222]]';
        if(file_exists($this->jsonSrc.'DragGraphicVerificationCodeCheck.json')){
            $txtData = file_get_contents($this->jsonSrc.'DragGraphicVerificationCodeCheck.json');
        }
        $codeData = json_decode($txtData,true);

        
        $filename = $codeData[$keyName.$_SERVER['REMOTE_ADDR']]['src'];

        $x = $codeData[$keyName.$_SERVER['REMOTE_ADDR']]['code'];
        $y = 130;

        // 重新取样
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefrompng($filename);
        imagecopyresampled($image_p, $image, -$x, -$y, 0, 0, 260*3, 160*3, 260*3, 160*3);

        // 输出
        imagejpeg($image_p, null, 100);

    }

    //校验code正确不正确
    public function checkCode($keyName,$code){
        $result = false;

        $txtData = '[]';
        if(file_exists($this->jsonSrc.'DragGraphicVerificationCodeCheck.json')){
            $txtData = file_get_contents($this->jsonSrc.'DragGraphicVerificationCodeCheck.json');

        }
        $codeData = json_decode($txtData,true);

        foreach ($codeData as $k=>$row){
            if($k == $keyName.$_SERVER['REMOTE_ADDR'] and abs($row['code']- $code) < 10 and $row['time'] >= time()){
                $result = true;
            }
        }

        return $result;

    }



}